package com.googlecode.scenemaker3d.controller
{
	import com.googlecode.scenemaker3d.model.VO_Camera3D;
	import com.googlecode.scenemaker3d.model.World3DModel;
	
	import mx.collections.ArrayCollection;
	
	import org.papervision3d.cameras.Camera3D;
	import org.papervision3d.core.math.Number3D;
	
	import com.googlecode.scenemaker3d.services.CameraViewType;
	import com.googlecode.scenemaker3d.services.Initialize;

	
	public class CameraController implements ICameraController
	{
		private var mWorld:World3DModel;
		
		public function CameraController(m:World3DModel)
		{	
			mWorld = m;	
		}
		public function addCamera(cam:Camera3D):VO_Camera3D
		{
			var VO:VO_Camera3D = new VO_Camera3D;
			VO.id = cam.name;
			VO.label = cam.name;
			VO.data = cam;
			VO.viewportZoom = Initialize.DEFAULT_CAMERA_ZOOM;
			
			var cameras:ArrayCollection = mWorld.cameras;
					
			cameras.addItem(VO);
			
			return VO;
		}
		public function removeCamera(id:String):void		
		{
			var cameras:ArrayCollection = mWorld.cameras;
			
			for(var i:int=0; i<cameras.length; i++){
				if(cameras.getItemAt(i).id == id){	
					cameras.removeItemAt(i);
					break;
				}
			}
		}
		public function getCameraAt(index:uint):VO_Camera3D
		{
			var cameras:ArrayCollection = mWorld.cameras;
			
			return (cameras.getItemAt(index) as VO_Camera3D);
		}
		public function getCameraByName(id:String):VO_Camera3D
		{
			var cameras:ArrayCollection = mWorld.cameras;
			
			for(var i:int=0; i<cameras.length; i++){
				if(cameras.getItemAt(i).id == id){	
					return (cameras.getItemAt(i) as VO_Camera3D);
				}
			}
			return null;
		}
		public function getCameraListByType(type:String):Array
		{
			var cameras:ArrayCollection = mWorld.cameras;
			var cameraList:Array = new Array();
			
			for(var i:int=0; i<cameras.length; i++)
			{
				var stub:String = String(cameras.getItemAt(i).id).split("_")[0];
				if(stub == type)
				{
					cameraList.push(cameras.getItemAt(i));
				}
			}
			return cameraList;
		}
		public function setCameraPosition(id:String, pos:Number3D):void
		{
		}
		
		public function setCameraZoom(id:String, zoom:Number):void
		{
			var cameras:ArrayCollection = mWorld.cameras;
			
			for(var i:int=0; i<cameras.length; i++)
			{
				if(cameras.getItemAt(i).id == id)
				{	
					cameras.getItemAt(i).viewportZoom = zoom;
					switch(id)
					{
						case CameraViewType.FRONT_VIEW:
							cameras.getItemAt(i).data.zoom = zoom * 200;
							
							break;
						case CameraViewType.LEFT_VIEW:
							cameras.getItemAt(i).data.zoom = zoom * 200;

							break;
						case CameraViewType.TOP_VIEW:
							cameras.getItemAt(i).data.zoom = zoom * 200;

							break;
						default:
							cameras.getItemAt(i).data.zoom = zoom;
							
					}
					break;
				}
			}
			cameras.itemUpdated(cameras.getItemAt(i).data, "zoom", null, zoom);		
		}
		
		public function setCameraFocus(name:String, focus:Number):void
		{
		}
	}
}